// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PartitionKeyResolver.cs" company="">
//   
// </copyright>
// <summary>
//   The partition key resolver.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace XRay.Library
{
    using System;

    using AutoMapper;

    using Microsoft.WindowsAzure.Storage.Table;

    /// <summary>
    /// The partition key resolver.
    /// </summary>
    public class PartitionKeyResolver : ValueResolver<TableEntity, DateTimeOffset>
    {
        #region Methods

        /// <summary>
        /// The resolve core.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <returns>
        /// The <see cref="DateTimeOffset"/>.
        /// </returns>
        protected override DateTimeOffset ResolveCore(TableEntity source)
        {
            // The partition key is in format : YYYYMMddThhmm where mm are 00
            var year = Convert.ToInt32(source.PartitionKey.Substring(0, 4));
            var month = Convert.ToInt32(source.PartitionKey.Substring(4, 2));
            var day = Convert.ToInt32(source.PartitionKey.Substring(6, 2));
            var hour = Convert.ToInt32(source.PartitionKey.Substring(9, 2));

            return new DateTimeOffset(year, month, day, hour, 0, 0, TimeSpan.FromHours(0));
        }

        #endregion
    }
}